#!/usr/sbin/rsct/perl5/bin/perl 
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 1999,2002 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# "@(#)48   1.14   src/rsct/registry/cli/bin/cpsr.perl, srcli, rsct_rpyxh, rpyxht1f3 2/22/01 16:25:52"
######################################################################
#                                                                    #
# Module: cpsr                                                       #
#                                                                    #
# Purpose:                                                           #
#   cpsr - Copy directorries and tables (entries) in the System      #
#          Registry.                                                 #
#                                                                    #
# Syntax:                                                            #
#   To copy a table to another table:                                #
#   cpsr [-h][-f][-TV] Source_table Target_table                     #
#                                                                    #
#   To copy a table to a directory:                                  #
#   cpsr [-h][-f][-TV] Source_table ... Target_directory             #
#                                                                    #
#   To copy a directory to a directory:                              #
#   cpsr [-h][-f][-TV] Source_directory ... Target_directory         #
#                                                                    #
# Flags:                                                             #
#   -h  Help. Writes this command's usage statement to stdout.       #
#   -f  Force. Does not prompt you before overwriting an existing    #
#       entry. Target removal precedes the copy.                     #
#   -T  Trace. Writes this command's trace messages to stderr.       #
#   -V  Verbose. Writes this command's verbose messages to stderr.   #
#                                                                    #
# Operands:                                                          #
#   Source_table    The path of the table you wish to copy. The      #
#           Source_table must contain a relative or absolute path    #
#           name for an existing System Registry table. One or more  #
#           Source_table operands can be specified if the target     #
#           entry is a directory.                                    #
#   Target_table    The table you wish to create or replace. The     #
#           Target_table must contain a relative or absolute path    #
#           name.                                                    #
#   Target_directory    The directory you wish to create or replace. #
#           The Target_directory must contain a relatvie or absolute #
#           path name.                                               #
#   Source_directory    The directory you wish to copy. The          #
#           Source_directory must contain a relative or absolute     #
#           path name for an existing System Registry directory.     #
#           One or more Source_directory operands can be specified.  #
#                                                                    #
# Description:                                                       #
#   The cpsr command copies the table or directory specified in the  #
#   Source_table or Source_directory operand to the destination      #
#   specified in the Target_table or Target_directory operands.      #
#                                                                    #
#   More than one Source_table or Source_directory can be copied     #
#   if the target entry is a directory. If more than one source is   #
#   specified, it is equivalent to the command being invoked once for#
#   each source.                                                     #
#                                                                    #
#   If any directories are created by the cpsr command during the    #
#   copying process, the newly created directory will have the same  #
#   storage characteristics as the corresponding source directory.   #
#                                                                    #
#   When you copy to an existing entry:                              #
#   1) If the target and source are tables, you will be prompted to  #
#   overwrite the existing target table unless the -f flag is used.  #
#   2) If the target is a table and the source a directory, the table#
#   will be copied into the new directory.                           #
#   3) If the target and source are directories, the source          #
#   directory and its contents are added under the existing target   #
#   directory unless the -f flag is used. In this case, the target   #
#   directory and it's entire subtree is deleted before the copy is  #
#   performed.                                                       #
#                                                                    #
#   The copied entry inherits the storage characteristics of it's    #
#   new parent directory.                                            #
#                                                                    #
#   Use the lssr -R command to get a recursive list of the current   #
#   System Registry directory structure.                             #
#                                                                    #
# Exit Values:                                                       #
#   0  SR_CLI_SUCCESS        Command completed successfully.         #
#   1  SR_CLI_REGISTRY_ERROR Command terminated due to an underlying #
#                            System Registry error.                  #
#   2  SR_CLI_ERROR          Command terminated due to an underlying #
#                            error in the command script.            #
#   3  SR_CLI_BAD_OPERAND    Command terminated due to user          #
#                            specifying a bad operand.               #
#   4  SR_CLI_BAD_FLAG       Command terminated due to user          #
#                            specifying an invalid flag.             #
#   5  SR_CLI_USER_ERROR     Command terminated due to a user error. #
#                            For example specifying an undefined     #
#                            table to be copied.                     #
#                                                                    #
# Examples:                                                          #
#   Assumption: the CT_SR_HOME environment variable is set to        #
#   /samples if a relative entry name is used in the following       #
#   examples.                                                        #
#                                                                    #
#   1. To copy the table Hosts to the directory /tmp/backup,         #
#   overwriting any existing copy of the table in the target, enter: #
#    cpsr -f Hosts /tmp/backup                                       #
#                                                                    #
#   2. To copy the directory tests and it's subtree to the directory #
#   /tmp/tests, when the target directory does not already exist,    #
#   enter:                                                           #
#    cp -R tests /tmp/tests                                          #
#                                                                    #
#   3. To copy the directory tests and it's subtree to the existing  #
#   directory path /tmp/tests, overwriting (replacing) the existing  #
#   directory, enter:                                                #
#    cp -Rf tests /tmp/tests                                         #
#                                                                    #
#   4. To copy the table Hosts to the table Hosts.bak, overwriting   #
#   the existing table, enter:                                       #
#    cp -f Hosts Hosts.bak                                           #
#                                                                    #
#   5. To copy the tables Hosts, Nodes and the directories test and  #
#   backup to the directory /tmp/garbage, overwriting any existing   #
#   matching entries, enter:                                         #
#    cp -Rf Hosts Nodes test backup /tmp/garbage                     #
#                                                                    #
#--------------------------------------------------------------------#
# Inputs:                                                            #
#                                                                    #
# Outputs:                                                           #
#   stdout - help/usage statement                                    #
#   stderr - trace, verbose and error messages                       #
#                                                                    #
# External Ref:                                                      #
#   Commands: $LSMSG                                                 #
#   Extensions:  CT::SR.pm CT::SRrc.pm CT::CT.pm                     #
#   Perl library routines: Getopts::Std                              #
#   SR cli routines: : SR_cli_utils.pm - init_session, isRelative    #
#                                    term_session, printCEMsg        #
#                                                                    #
# Tab Settings:                                                      #
#   4 and tabs should be expanded to spaces before saving this file. #
#   in vi:  (:set ts=4  and   :%!expand -4)                          #
#                                                                    #
# Change Activity:                                                   #
#   000929 HGJ 38317: Initial delivery.                              #
#                                                                    #
######################################################################

#--------------------------------------------------------------------#
#                                                                    #
# General Program Flow/Logic:                                        #
#                                                                    #
# A: Parse command line - get table name and other flags, operands   #
# B: Initialise session with registry, including changing the        #
#    current directory if a relative path name is given (use value   #
#    given in CT_SR_HOME)                                            #
# C: Open file to be edited - exit if it won't open                  #
# D: Process the input data, creating either field_t and             #
#    ct_char_ptr_t or field_array_t and string_array_t for single or #
#    multiple changes                                                #
# E: Call CT::SR::set_field_by_key or CT::SR::fields_by_key          #
#    depending on number of input pairs on the command line          #
# F: Close session table and tree                                    #
#                                                                    #
#--------------------------------------------------------------------#


#--------------------------------------------------------------------#
# Included libraries and extensions                                  #
#--------------------------------------------------------------------#
use lib "/usr/sbin/rsct/pm";
use locale;
use Getopt::Std;
use FileHandle;

use CT::CT qw(:ct_data_type_t);
use CT_cli_utils qw(printIMsg
                    printEMsg
);

use CT::SRrc;
use CT::SR;
use CT::SR qw(:sr_qualifier_t);
use SR_cli_utils qw(init_session    isRelative  term_session 
                    printCEMsg      printCIMsg  
                    $DEFAULT_GLOBAL_MOUNT_POINT
                    error_exit      set_session_variables
); 
use SR_cli_rc qw(:return_codes);


#--------------------------------------------------------------------#
# Global Variables                                                   #
#--------------------------------------------------------------------#
# Global constant values 
$TRUE           = 1;
$FALSE          = 0;

# Other variables
$Verbose        = $FALSE;               # -V flag : see ($opt_V)
$Trace          = $FALSE;               # -T flag : see ($opt_T)
$Opt_Overwrite  = $FALSE;               # -f flag : see ($opt_f)

# Messaging variables
$PROGNAME       = "cpsr";
$MSGCAT         = "srcli.cat";
$CTDIR          = "/usr/sbin/rsct";     # Cluster directory path
$CTBINDIR       = "$CTDIR/bin";         # Cluster Bin directory path
$LSMSG          = "$CTBINDIR/ctdspmsg"; # Display message routine
$ENV{'MSGMAPPATH'} = "$CTDIR/msgmaps";  # Msg map path for $LSMSG  

%Cleanup = ();                            # Hash of items to cleanup
                                          # {Session} $session to term

#--------------------------------------------------------------------#
# Variables                                                          #
#--------------------------------------------------------------------#
# Variables for use with extensions
my $Tree_handle    = "";                # initialised in initSession

# Other variables
my @Source_entry   = ();                # list of source entries
my @Source         = ();                # fully qualified source(s)
my $Target_entry   = "";                # target of move
my $Target         = "";                # fully qualified target
my $Set_work_dir   = $FALSE;            # flag whether to set SR CWD
my $rc             = 0;                 # assume good return code
my $badrc          = 0;                 # assume good return code
my $Overwrite      = $FALSE;            # Used to toggle overwrite arg
my $redo           = $FALSE;            # redo a copy if overwrite
my $Mount_point    = $DEFAULT_GLOBAL_MOUNT_POINT;


#--------------------------------------------------------------------#
# Main Code                                                          #
#--------------------------------------------------------------------#
# TODO: Many verbose statements in this code will eventually by
# Trace statements when the facility is available as a Perl CLI
# (feature 48401)
# TODO: security on access to the table can not be further defined
# until after a security design has been implemented for the SR.
# ( feature 48402 ) Until then, the table is opened with the
# minimum security necessary to complete the command.
# TODO: FFDC handling will be implemented in Feature 48397.

# Parse the command line, exit if there are errors
($rc, $Target_entry, @Source_entry) = parse_cmd_line();

# Error messages handled in parse_cmd_line
($rc == 0) || error_exit($rc);  

if ($Verbose) {
    foreach (@Source_entry) {
        $Column_list .= "  ".$_."\n";
    }
    $Command_line_input = "\"  $Column_list\n  $Target_entry\n\"";
    printIMsg("IMsgcpsrCommandLineInput", $Command_line_input);
}


# Initialize variables for init_session - check to see if an
# absolute or relative entry name has been passed in.
# Prepare each entry with the full mount point or not, 
# depending on whether it was a relative entry or absolute.

($Set_work_dir, $Target) = set_session_variables($Target_entry);

$ind = 0;
foreach (@Source_entry) {
    ($holder, $Source[$ind++]) = set_session_variables($_);
    if ($holder == $TRUE) {$Set_work_dir = $TRUE};
}


# Initialize Registry Library Connection
# Error messages handled in initSession
($rc, $Tree_handle) = init_session($Set_work_dir);
($rc == 0) || error_exit($rc);  

# Add tree handle to cleanup hash
$Cleanup{Session} = $Tree_handle;


# Check the copies for logic, and report errors as necessary

$Trace && print STDERR "Entering check_copies\n";
($rc, $Target_type) = 
    check_copies($Tree_handle, $Target, @Source);
$Trace && 
    print STDERR "check_copies return code ($rc, $Target_type)\n";

# Error messages handled in check_copies
($rc == 0) || error_exit($rc);  


# Attempt to copy each source given to the target
$ind = 0;
foreach $source (@Source) {
    if ($Target_entry eq $Source_entry[$ind]) {
        printCEMsg("EMsgSRcliIdenticalEntries", 
                        $Target_entry, $Source_entry[$ind]);
        $rc = SR_CLI_BAD_OPERAND;
        $ind++;
        next;
    }   

    # If more than one source is defined, the correct name must
    # be used for messages and prompts
    my $entry = "";

    if ($Target_type eq 'dir') {    

        # Create the correct target name based on the entries given.
        # Absolute names are used in the command, but this is for 
        # messaging and prompting. 
        $entry = $Target_entry.'/'. strip_name($Target_entry, $Source_entry[$ind]);

    }
    else { $entry = $Target_entry; }


    # If a redo has been called, then a 'y' answer was received 
    # from the prompt 

    if ($redo or $Opt_Overwrite) { $Overwrite = $TRUE}
    else { $Overwrite = $FALSE; }


    $Verbose && 
        printIMsg("IMsgcpsrCopyingEntry", $Source_entry[$ind], $entry,
                    $Overwrite);

    $Trace && print STDERR "Calling CT::SR::copy_entry($source, $Target, $Overwrite)\n";
    # Call the extension proper
    $rc = CT::SR::copy_entry($Tree_handle, $source, $Target, 
                        $Overwrite);

    $Trace && print STDERR "CT::SR::copy_entry return code $rc\n";

    # Reset the redo flag so the same entry isn't process AGAIN
    $redo = $FALSE if $redo;
    if ($rc == SR_ENTRY_EXISTS) {
        # Prompt and redo this attempt if requested
        printIMsg("IMsgcpsrOverwrite", $entry); 

        # Read from STDIN one line. As long as it begins
        # with a 'y' or 'Y', redo the last move
        # TODO: investigate using LC_MESSAGES for NLS
        # local compliance.

        $fh = new FileHandle "-";
        $answer = $fh->getline;
        $fh->close;

        if (($answer =~ /^y/) or ($answer =~ /^Y/)) { 
            $redo = $TRUE;
            $answer = "";
            $rc = 0;
            redo; 
        }
    }

    $rc = error_check("sr_copy_entry", $rc, $Source_entry[$ind], 
        $entry);
    if ($rc == SR_CLI_REGISTRY_ERROR) {
        $badrc = $rc;   
        last;  
    }
    # Save the first bad return code 
    if ($rc != 0 && $badrc == 0) { $badrc = $rc; }

    $ind++;
} # end foreach $source (@Source)

# $badrc is used here because varying user errors could exist
# for different operands on the command line.
($badrc == 0) || error_exit($badrc);

# Clean session before quitting
$rc = term_session($Tree_handle, $Mount_point);   

exit $rc;

#--------------------------------------------------------------------#
# End Main Code                                                      #
#--------------------------------------------------------------------#


#--------------------------------------------------------------------#
# check_copies:                                                      #
#   Checks the copy commands given for logic. Checks for:            #
#   - greater than one source sent to a table target                 #
#   - greater than one source sent to a non-existing target (assumes #
#   it is a table and returns an error)                              #
#   - source directory being copied to table target                  #
#   All the above cases return an error code of SR_CLI_BAD_OPERAND.  #
#                                                                    #
# Parameters:                                                        #
#   $tree_handle  - session tree handle                              #
#   $target       - the target entry to be compared with             #
#   @source - array of source entry names to be checked against targ.#
#                                                                    #
# Return values:                                                     #
#   $local_rc - 0 unless one of the checks matches                   #
#                                                                    #
# Globals used:                                                      #
#   None.                                                            #
#--------------------------------------------------------------------#
sub check_copies 
{
# Grab input parameters
my ($tree_handle, $target, @source) = @_;

# Set up local variables
my $target_type = 'none';
my $source_type = 'none';
my $local_rc = 0;

my $display_target = $target;
$display_target =~ s /^$DEFAULT_GLOBAL_MOUNT_POINT//;

# Check the Target type first

$Trace && print STDERR "Entering get_entry_type($target)\n";
($local_rc, $target_type) = get_entry_type($target, $tree_handle);
$Trace && 
   print STDERR "get_entry_type return code ($local_rc, $target_type)\n";

# Error messages handled in get_entry_type
$local_rc and return $local_rc;


if ($Verbose) {
    if ($target_type eq 'dir') { 
        printCIMsg("IMsgSRcliDirTarget", $display_target); 
    }
    else { 
        printCIMsg("IMsgSRcliTableTarget", $display_target);
    }
}


# Like AIX cp, if there are multiple tables given to be copied
# to a single table, burp out the usage statement and quit
if (($target_type eq 'table') and ($#source > 0)) {
    print_usage();
    return SR_CLI_BAD_OPERAND;
}


# If the target entry does not exist and multiple entries are given
# to copy, print the Usage statement and exit (like cp)
if (($target_type eq 'none') and ($#source > 0)) {
    print_usage();
    return SR_CLI_BAD_OPERAND;
}


# Check each source type against the target type.
# If an attempt is made to copy a directory to a table, the command 
# stops
foreach (@source) {
    $Trace && print STDERR "Entering get_entry_type($_)\n";
    ($local_rc, $source_type) = get_entry_type($_, $tree_handle);
    $Trace && print STDERR "get_entry_type return code ($local_rc, $source_type)\n";

    # Strip off any existing leading '/SR' for display purposes
    $display_source = $_;
    $display_source  =~ s /^$DEFAULT_GLOBAL_MOUNT_POINT//;

    if ($Verbose) {
        if ($source_type eq 'dir') {
            printCIMsg("IMsgSRcliDirSource", $display_source); 
        }
        else {
            printCIMsg("IMsgSRcliTableSource", $display_source); 
        }
    }

    if (($target_type eq 'table') and ($source_type eq 'dir')) {
        printEMsg("EMsgcpsrTableToDir", $display_source , 
                        $display_target);
        return SR_CLI_BAD_OPERAND;
    }
    
}

return $local_rc, $target_type;
}   # end check_copies


#--------------------------------------------------------------------#
# strip_name:                                                        #
#   if the target_entry and source have a common directory path,     #
#   the source is stripped of this path so a correct source path     #
#   can be constructed for messaging and prompting purposes.         #
#                                                                    #
# Parameters:                                                        #
#   $target_entry - the target entry to be compared with             #
#   $source - the source entry name to be compared and stripped      #
#                                                                    #
# Return values:                                                     #
#   $source - the stripped source value.                             #
#--------------------------------------------------------------------#
sub strip_name
{
my ($target_entry, $source)  = @_;
my $target = $target_entry;

my $equal = 1;

while (($target =~ /\//) and $equal) {
    $dir = $target;
    $dir =~ s/^(.*)\/.*/$1/;
    $target =~ s/^.*\/(.*)/$1/;
    if (!($source =~ s/^$dir//)) { $equal = 0; }
}

# Strip off any leading '/' so a relative entry is given back
$source =~ s/^\///;

return $source;
}   # end strip_name


#--------------------------------------------------------------------#
# get_entry_type:                                                    #
#   Uses CT::SR::is_in_directory to verify if the target exists, and #
#   if it does to return the entry type.                             #
#                                                                    #
# Parameters:                                                        #
#   $target - the target entry to be worked on                       #
#   $tree - valid tree handle for the library session                #
#   $relative - boolean flag indicating if the target is relative    #
#                                                                    #
# Return values:                                                     #
#   $local_rc - local return code                                    #
#   $type - '', 'none', 'table', 'dir', depending on outcome of      #
#    the CT::SR::is_in_directory call                                #
#                                                                    #
# Globals used:                                                      #
#   None.                                                            #
#--------------------------------------------------------------------#
sub get_entry_type 
{
# Have to include this for properly checking the target type if
# a relative target has been used.
use Env qw(CT_SR_HOME);
use CT::SR qw(:sr_entry_t);

#Grab input parameters
my ($target, $tree) = @_;
  
# Set up local variables
my ($temp_dir, $parent_dir, $entry);
my $type = 0;
my $storage = 0;
my $local_rc = 0;
 
 
# Strip off /SR for use in any possible messaging
# (value is internal to the CLI.)
$temp_dir = $target;
$temp_dir =~ s/^$DEFAULT_GLOBAL_MOUNT_POINT//;

if (isRelative($target)) { 
    $target = "$DEFAULT_GLOBAL_MOUNT_POINT".$CT_SR_HOME.'/'.$target; 
}

 
# Split off the right most entry in the current directory so
# there is a parent directory and entry to pass to 
# CT::SR:is_in_directory
if ($target =~ /\//) {  
    $parent_dir = $target;
    $parent_dir =~ s/(.*)\/.*$/$1/;
  
    $entry = $target;
    $entry =~ s/.*\/(.*)$/$1/;
}
else {
    $parent_dir = $DEFAULT_GLOBAL_MOUNT_POINT; 
    $entry = $target;
}


$Trace && print STDERR "Calling CT::SR::is_in_directory($parent_dir, $entry)\n";
$local_rc = CT::SR::is_in_directory($tree, $parent_dir, $entry, 
                        $type, $storage);
$Trace && 
    print STDERR "CT::SR::is_in_directory return code: $local_rc\n";

  
if ($local_rc != 0) {
    if (($local_rc == SR_NO_DIRECTORY) or ($local_rc == SR_NO_ENTRY)){
        # Entry is new, so can simply keep going without a type
        $local_rc = 0;
        $type = 'none';
    }
    elsif ($local_rc == SR_NO_PERMISSION) {
        printEMsg("EMsgcpsrNoPermissionTarget", $target);
        return SR_CLI_USER_ERROR;
    }
    else {
        printEMsg("EMsgSRcliErrorValidatingTarget");
        printCEMsg("EMsgSRcliSRCommandFailure", 
                            "sr_is_in_directory", $local_rc);
        $local_rc = SR_CLI_REGISTRY_ERROR;
    }
    return $local_rc, $type;
}
    
# Verify that the entry checked is not a table
# If it is an unknown type, is considered a severe Registry Error
if ($type == SR_TABLE){
    $type = 'table';
}
elsif ($type == SR_DIRECTORY){
    $type = 'dir';
}
elsif ($type == SR_UNKNOWN_ENTRY) {
    $Verbose && print STDERR "Entry type is $type\n";
    printEMsg("EMsgcpsrUnknownEntry", $temp_dir);
    $local_rc = SR_CLI_REGISTRY_ERROR;
}
    
return $local_rc, $type;
}   # get_entry_type


#--------------------------------------------------------------------#
# parse_cmd_line:                                                    #
#   Uses getopts() to grab flags on the command line. parses the     #
#   command data, (checks the syntax of the key_column value and     #
#   otherwise makes sure there is data to be changed) then returns   #
#   the table to be modified, the count of changes to be made and    #
#   the columns to be changed.                                       #
#                                                                    #
# Return values:                                                     #
#   $local_rc - local return code                                    #
#   $table_name - table name to be modified                          #
#   $count - count of columns to be modified                         #
#   @columns - array containing column=value pairs, with the first   #
#              entry being the key_column==value necessary for       #
#              matching the row to be edited                         #
#                                                                    #
# Global variables modified:                                         #
#   $Trace             output   True (-T) turn Trace mode on.        #
#   $Verbose           output   True (-V) turn Verbose mode on.      #
#   $Opt_Overwrite     output   True (-f) force over write           #
#--------------------------------------------------------------------#
sub parse_cmd_line
{
my $local_rc = 0;
my @columns = ();
my $table_name = "";
my $count = 0;
my %opts = ();

if (getopts('hfTV', \%opts) == 0) {      # Parse input flags
    printCEMsg("EMsgSRcliInvalidFlag");
    print_usage();
    return SR_CLI_BAD_FLAG;
}                  

# Print usage and exit if -h used
if (defined $opts{h}) {   
    print_usage();
    exit(0);    
}

# Set Trace flag if requested
if (defined $opts{T}) {
    $Trace = $TRUE;
}

# Set Verbose flag if requested
if (defined $opts{V}) {
    $Verbose = $TRUE;
}

# Set $Opt_Overwrite flag if requested
if (defined $opts{f}) {
    $Opt_Overwrite = $TRUE;
}

# Do a basic check on the count of arguments
if (scalar(@ARGV) < 2) {
    printCEMsg("EMsgSRcliMissingOperands");
    print_usage();
    return SR_CLI_BAD_OPERAND;
}

# Get target entry name from input.
# pop the last entry of @ARGV, assuming this is the table name
$target_name = pop @ARGV;

# Assign the Source entries back to the arg list via @ARGV
return ($local_rc, $target_name, @ARGV);
}   # end parse_cmd_line


#--------------------------------------------------------------------#
# error_check:                                                       #
#   Checks the return code from the SR function.  If an error is     #
#   detected appropriate error messages will be displayed and        #
#   SR CLI return code set.                                          #
#                                                                    #
# Parameters:                                                        #
#   $sr_function  - Name of the SR function that was called and      #
#                   whose error code we are checking.                #
#   $sr_rc        - SR function return code.                         #
#   $source_entry - One of the source entries for the copy.          #
#   $target_entry - one of the target entries for the copy.          #
#                                                                    #
# Return values:                                                     #
#   $rc           - SR C API rc converted to a SR CLI rc.            #
#                                                                    #
# Global References:                                                 #
#   None.                                                            #
#--------------------------------------------------------------------#
sub error_check
{
my ($sr_function, $sr_rc, $source_entry, $target_entry) = @_;
my $rc = 0;

if ($sr_rc != 0) {
    if ($sr_rc == SR_CONNECTION_LOST) {
        # Quit the program - nothing else will occur this session
        # The registry will not respond to session termination
        # calls at this point.
        printCEMsg("EMsgSRcliConnectionLost");
        $rc = SR_CLI_REGISTRY_ERROR;
    }
# TODO: Defect 71605, registry component, only returning 
# SR_NO_PERMISSION not the .._SOURCE or .._TARGET
# Also in ct_sr_rc.h they are defined as SR_NO_PERMISSION_TA
# or SR_NO_PERMISSION_SO
    elsif ($sr_rc == SR_NO_PERMISSION) {
        printEMsg("EMsgmvsrNoPermissionSource", $source_entry);
        printEMsg("EMsgmvsrNoPermissionTarget", $target_entry);
        $rc = SR_CLI_USER_ERROR;
    }
#   elsif ($sr_rc == SR_NO_PERMISSION_SOURCE) {
#       printEMsg("EMsgcpsrNoPermissionSource", $source_entry);
#       $rc = SR_CLI_USER_ERROR;
#   }
#   elsif ($sr_rc == SR_NO_PERMISSION_TARGET) {
#       printEMsg("EMsgcpsrNoPermissionTarget", $target_entry);
#       $rc = SR_CLI_USER_ERROR;
#   }
    elsif ($sr_rc == SR_NO_DIRECTORY) {
        printCEMsg("EMsgSRcliNoDirectory", $target_entry);
        $rc = SR_CLI_USER_ERROR;
    }
    elsif ($sr_rc == SR_NO_ENTRY) {
        printEMsg("EMsgcpsrNoEntry", $source_entry);
        $rc = SR_CLI_USER_ERROR;
    }
    elsif ($sr_rc == SR_INVALID_OVERWRITE) {
        printEMsg("EMsgcpsrInvalidOverwrite");
        $rc = SR_CLI_ERROR;
    }
    elsif ($sr_rc == SR_ENTRY_EXISTS) {
        printEMsg("EMsgcpsrEntryExists", $target_entry);
        $rc = SR_CLI_USER_ERROR;
    }
    else {
        # If the error is in this category, just quit.
        printEMsg("EMsgcpsrErrorCopyingEntry", $source_entry, 
            $target_entry);
        printCEMsg("EMsgSRcliSRCommandFailure", $sr_function, $sr_rc);
        $rc = SR_CLI_REGISTRY_ERROR;
    }
}   

return ($rc);
}   # end error_check

#--------------------------------------------------------------------#
# print_usage : print the usage statement (syntax) to stdout.        #
#   See this command's prologue syntax section for current usage.    #
#--------------------------------------------------------------------#
sub print_usage
{
printIMsg("IMsgcpsrUsage");
}   # end print_usage


#--------------------------------------------------------------------#
# End File                                                           #
#--------------------------------------------------------------------#
